package com.star.percalservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.percalservice.mapper.*;
import com.star.percalservice.model.dto.TeacherDTO;
import com.star.percalservice.model.entity.*;
import com.star.percalservice.model.vo.TeacherDataVO;
import com.star.percalservice.service.ScoreRuleService;
import com.star.percalservice.service.TeacherService;
import com.star.percalservice.utils.R;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author star
 * @version 1.0
 * @Description:
 * @DateTime 2025/4/13 2:45
 */
@Service
public class TeacherServiceImpl implements TeacherService{

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ResearchProjectMapper researchProjectMapper;
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PatentMapper patentMapper;
    @Autowired
    private AwardMapper awardMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScoreRuleService scoreRuleService;

    @Override
    public R getTeacherDataById(Integer id) {
        Teacher teacher = teacherMapper.selectById(id);
        if (teacher == null) {
            return R.error("教师不存在");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("teacher", teacher);
        return R.ok("获取教师信息成功", map);
    }

    @Override
    public R list(TeacherDTO teacherDTO) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        if (Strings.hasLength(teacherDTO.getName())) {
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.like("name", teacherDTO.getName());
            List<User> users = userMapper.selectList(userQueryWrapper);
            List<Integer> userIds = users.stream().map(User::getId).toList();
            teacherQueryWrapper.in("user_id", userIds);
        }
        if (Strings.hasLength(teacherDTO.getDepartment())) {
            teacherQueryWrapper.like("department", teacherDTO.getDepartment());
        }
        if (Strings.hasLength(teacherDTO.getTitle())) {
            teacherQueryWrapper.like("title", teacherDTO.getTitle());
        }
        List<Teacher> teacherList = teacherMapper.selectList(teacherQueryWrapper);
        List<TeacherDataVO> teacherDataVOList = new ArrayList<>();
        teacherList.forEach(teacher -> {
            TeacherDataVO teacherDataVO = new TeacherDataVO();
            User user = userMapper.selectById(teacher.getUserId());
            QueryWrapper<ResearchProject> researchProjectQueryWrapper = new QueryWrapper<>();
            researchProjectQueryWrapper.eq("teacher_id", teacher.getId());
            List<ResearchProject> projects = researchProjectMapper.selectList(researchProjectQueryWrapper);
            QueryWrapper<Paper> paperQueryWrapper = new QueryWrapper<>();
            paperQueryWrapper.eq("teacher_id", teacher.getId());
            List<Paper> papers = paperMapper.selectList(paperQueryWrapper);
            QueryWrapper<Patent> patentQueryWrapper = new QueryWrapper<>();
            patentQueryWrapper.eq("teacher_id", teacher.getId());
            List<Patent> patents = patentMapper.selectList(patentQueryWrapper);
            QueryWrapper<Award> awardQueryWrapper = new QueryWrapper<>();
            awardQueryWrapper.eq("teacher_id", teacher.getId());
            List<Award> awards = awardMapper.selectList(awardQueryWrapper);
            Double score = scoreRuleService.calculateScore(teacher.getId());
            teacherDataVO.setUser(user);
            teacherDataVO.setTeacher(teacher);
            teacherDataVO.setProjects(projects);
            teacherDataVO.setPapers(papers);
            teacherDataVO.setPatents(patents);
            teacherDataVO.setAwards(awards);
            teacherDataVO.setScore(score);
            teacherDataVOList.add(teacherDataVO);
        });
        return R.ok("获取教师列表成功", teacherDataVOList);
    }

    @Override
    public R add(Teacher teacher) {
        return teacherMapper.insert(teacher) > 0 ? R.ok("添加教师成功") : R.error("添加教师失败");
    }

    @Override
    public R update(Teacher teacher) {
        return teacherMapper.updateById(teacher) > 0 ? R.ok("修改教师成功") : R.error("修改教师失败");
    }

    @Override
    public R delete(Integer id) {
        // 删除教师之前，需要删除教师关联的论文、专利、获奖等数据
        paperMapper.delete(new QueryWrapper<Paper>().eq("teacher_id", id));
        patentMapper.delete(new QueryWrapper<Patent>().eq("teacher_id", id));
        awardMapper.delete(new QueryWrapper<Award>().eq("teacher_id", id));
        researchProjectMapper.delete(new QueryWrapper<ResearchProject>().eq("teacher_id", id));
        userMapper.delete(new QueryWrapper<User>().eq("id", id));
        return teacherMapper.deleteById(id) > 0 ? R.ok("删除教师成功") : R.error("删除教师失败");
    }

    @Override
    public R getByUserId(Integer userId) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id", userId);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("teacher", teacher);
        return teacher == null ? R.error("教师不存在") : R.ok("获取教师信息成功", map);
    }
}
